package com.lyj.system.service.sysUser.ServiceImpl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lyj.common.core.constants.Constants;
import com.lyj.common.core.constants.HttpConstants;
import com.lyj.common.core.domain.LoginUser;
import com.lyj.common.core.domain.R;
import com.lyj.common.core.enums.ResultCode;
import com.lyj.common.core.enums.UserIdentity;
import com.lyj.security.service.TokenService;
import com.lyj.system.domain.sysUser.SysUser;
import com.lyj.system.domain.sysUser.dto.SysUserSaveDTO;
import com.lyj.system.mapper.sysUser.SysUserMapper;
import com.lyj.system.service.sysUser.SysUserService;
import com.lyj.system.utils.BCryptUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.lyj.common.core.domain.vo.LoginUserVO;
import com.lyj.security.exception.ServiceException;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LIUYANJIAO
 * Date: 2025-08-12
 * Time: 23:58
 */
@Service
public class SysUserServiceImpl implements SysUserService {
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private TokenService tokenService;
    @Value("${jwt.secret}")
    private String secret;

    @Override
    public R<String> login(String userAccount, String password) {
        //判断是否存在账号
        //密码是否相同
        //存储token和redis
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        SysUser sysUser = sysUserMapper.selectOne(queryWrapper.lambda().
                select(SysUser::getUserId,SysUser::getNickName,SysUser::getPassword).
                eq(SysUser::getUserAccount,userAccount));
        if(sysUser == null){
            return R.fail(ResultCode.FAILED_USER_NOT_EXISTS);
        }
        if(BCryptUtils.matchesPassword(password,sysUser.getPassword())){
            return R.ok(tokenService.createToken(sysUser.getUserId(),
                    secret, UserIdentity.ADMIN.getValue(), sysUser.getNickName(), null));
        }
        return R.fail(ResultCode.FAILED_LOGIN);
    }

    @Override
    public boolean logout(String token) {
        if(StrUtil.isNotEmpty(token) && token.startsWith(HttpConstants.PREFIX)){
            token = token.replaceFirst(HttpConstants.PREFIX,StrUtil.EMPTY);
        }
        return tokenService.deleteLoginUser(token,secret);
    }

    @Override
    public R<LoginUserVO> info(String token) {
        if(StrUtil.isNotEmpty(token) && token.startsWith(HttpConstants.PREFIX)){
            token = token.replaceFirst(HttpConstants.PREFIX,StrUtil.EMPTY);
        }
        LoginUser loginUser = tokenService.getLoginUser(token, secret);
        if(loginUser == null){
            return R.fail();
        }
        LoginUserVO loginUserVo = new LoginUserVO();
        loginUserVo.setNickName(loginUser.getNickName());
        return R.ok(loginUserVo);
    }

    @Override
    public int add(SysUserSaveDTO sysUserSaveDTO) {
        //判断账号是否已经存在
        List<SysUser> sysUsers = sysUserMapper.selectList(new QueryWrapper<SysUser>().lambda()
                .eq(SysUser::getUserAccount,sysUserSaveDTO.getUserAccount()));
        if(CollectionUtil.isNotEmpty(sysUsers)){
            //说明用户已存在
            throw new ServiceException(ResultCode.FAILED_USER_EXISTS);
        }

        //添加管理员
        SysUser sysUser = new SysUser();
        sysUser.setUserAccount(sysUserSaveDTO.getUserAccount());
        sysUser.setPassword(BCryptUtils.encryptPassword(sysUserSaveDTO.getPassword()));
        sysUser.setCreateBy(Constants.SYSTEM_USER_ID);

        return sysUserMapper.insert(sysUser);
    }

}
